package com.md.youyouhui.ui.home;

import android.databinding.DataBindingUtil;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.md.youyouhui.R;
import com.md.youyouhui.databinding.ActivityActMoreBinding;

import com.md.youyouhui.databinding.AdapterActBinding;
import com.md.youyouhui.net.response.IndexResponse;
import com.md.youyouhui.ui.BaseActivity;
import com.md.youyouhui.ui.adapter.DataBoundViewHolder;
import com.md.youyouhui.utils.GlideApp;
import com.md.youyouhui.utils.ToastUtils;
import com.md.youyouhui.view.MainViewModel;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by AdministratorZJ
 * on 2019/5/21.
 */
public class ActMoreActivity extends BaseActivity {
    private ActivityActMoreBinding moreBinding;
    private MyAdapter adapter;
    private MainViewModel viewModel;

    @Override
    public void setContentLayout() {
        moreBinding = DataBindingUtil.setContentView(this, R.layout.activity_act_more);
        viewModel = new MainViewModel();
    }

    @Override
    public void initView() {
        String title = getIntent().getStringExtra("title");
        moreBinding.toolbar.setTitle(title);
        setSupportActionBar(moreBinding.toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    }

    @Override
    public void initData() {
        String id = getIntent().getStringExtra("id");
        adapter = new MyAdapter();
        moreBinding.moreRecycler.setAdapter(adapter);
        getInfo();
    }
    private List<String> images = new ArrayList<>();
    private void getInfo() {
        viewModel.index(1, 5).observe(this, listResource -> {
            assert listResource != null;
            switch (listResource.status) {
                case LOADING:
                    break;
                case SUCCESS:
                    IndexResponse data = listResource.data;

                    if (null == data) {
                        return;
                    }
                    for (int i = 0; i < data.rows.size(); i++) {
                        images.add(data.rows.get(i).cover);

                    }
                    Log.e("urls",images.get(0));
                    adapter.upDate(images);
                    break;
                case ERROR:
                    ToastUtils.errMake(this, listResource.errorCode);
                    break;
                default:
                    break;
            }
        });
    }

    class MyAdapter extends RecyclerView.Adapter<DataBoundViewHolder<AdapterActBinding>> {
        List<String> datas;

        void upDate(List<String> data) {
            this.datas = data;
            notifyDataSetChanged();
        }

        @NonNull
        @Override
        public DataBoundViewHolder<AdapterActBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            AdapterActBinding binding = DataBindingUtil.inflate(LayoutInflater.from(ct), R.layout.adapter_act, viewGroup, false);
            return new DataBoundViewHolder<>(binding);
        }

        @Override
        public void onBindViewHolder(@NonNull DataBoundViewHolder<AdapterActBinding> holder, int i) {
            GlideApp.with(ct).load(datas.get(i)).into(holder.binding.infoImage);
        }

        @Override
        public int getItemCount() {
            return null == datas ? 0 : datas.size();
        }
    }
}
